    <div class="head"><h5 class="iList">Category Levels for <?php echo $directory->getName() ?></h5></div>
     
     		<div class="fix"></div>
     <form action="<?php echo url_for('category_levels/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" id="valid" class="mainForm" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
     	
     
        <div class="rowElem noborder">
     	<label for="name">Category:</label>
     		<div class="formRight" >
                    			<select name="level[category_id]" id="level_category_id" onchange="loadCategoryData()"  style="width:240px;"  >
                    				<?php 
                    					
                    					foreach ($categories as $aCategory) {
                    						
                    					 $selected = $aCategory->getId() == $form->getObject()->getCategoryId() ? "selected" : "";
                    					?>
                    						<option <?php echo $selected?> value="<?php echo $aCategory->getId(); ?>"><?php echo $aCategory->getName(); ?></option>
                    					<?php 
                    					}
                    				?>
                            	</select>     		
     		
     		</div>
     		<div class="fix"></div>
     	</div>
     	
     
     	<div class="rowElem noborder">
     	<label for="name">Name:</label>
     		<div class="formRight"><input type="text" name="level[name]" value="<?php echo $form->getObject()->getName()?>"  class="validate[required]"  id="level_name" style="width:150px;width: 231px;"/></div>
     		<div class="fix"></div>
     	</div>
     	

     	<div class="rowElem noborder" >
     	<label for="name">Parent level:</label>
     		<div class="formRight" >
                    			<select name="level[parent_id]" id="level_parent_id" style="width:240px;" class="abc">
                    			
                    				
                    				<?php 
                    					if (count($levels) == 0 || ($form->getObject()->getParentId() == null && !$form->getObject()->isNew() )){
                    					?>
                    					<option value="">--None--</option>
                    					<?php 	
                    					}
                    					else{
                    						
                    					foreach ($levels as $aLevel) {
                    						if ($aLevel->getId() != $form->getObject()->getId()){
                    						 $selected = $aLevel->getId() == $form->getObject()->getParentId() ? "selected" : "";
                    					?>
                    						<option <?php echo $selected?> value="<?php echo $aLevel->getId(); ?>"><?php echo $aLevel->getName(); ?></option>
                    					<?php 
                    						}
                    					}
                    					}
                    				?>
                            	</select>         		
     		
     		</div>
     	</div>
     	
     	<div class="rowElem noborder" >
     	

   	
     		<div class="formRight" >
     		
               <input type="button" value="Save" class="greyishBtn submitForm" id="save" />
               
     		<?php if (!$form->getObject()->isNew()){?>
     			<input type="button" value="Delete" class="redBtn submitForm" id="delete" />
     		
     		<?php } ?>                 
               
     		</div>
     	</div>     	
     	
     	
     </form>
     

     
<script type="text/javascript">

$("#save").click(function (event){
    submitForm();
})

$("form#valid").submit(function (event){
    event.preventDefault();
    submitForm();
})

function submitForm(){
    var str=$('form#valid').serialize();

    $.ajax({type: 'POST', url:' <?php echo url_for('category_levels/'.($form->getObject()->isNew() ? 'create' :'update').
    (!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : ''))?>', script:true, cache:false, data:  str, 
    beforeSend: function(){
        if (!validate()) return false; },
            success: function(data) {
                 $('#category_levels_container_div').html(data);
                 $('#level_name').attr('value',"");
             } });
}


		
		$("#delete").click(function (event){

			jConfirm('Are you sure you want to delete the selected Level?', 'Confirmation Dialog', function(r){
				if (!r){
					return false;
				} else {
					var php_url = '<?php echo url_for('category_levels/delete?id='.$form->getObject()->getId())?>';
					$.ajax({type: 'POST', url: php_url, script:true, cache:false, 
						success: function(data) { $('#category_levels_container_div').html(data);
												  $('#level_name').attr('value',""); } })
				}
			})
		}
		);




		function loadCategoryData (){

			var php_url = '<?php echo url_for('category_levels/loadCategoryData?id=')?>';
			var category_id = document.getElementById('level_category_id').value;
			php_url = php_url.concat(category_id);	
			
			$.ajax({type: 'POST', url:php_url, script:true, cache:false, 
				success: function(data) { 
					 $('#category_levels_container_div').html(data);
					 $('#level_name').attr('value',"");
					} });
			
		}
		
		function loadCategories (){

			var php_url = '<?php echo url_for('category_levels/loadCategories?directory_id=')?>';
			var directory_id = document.getElementById('directory').value;
			php_url = php_url.concat(directory_id);	
			
			$.ajax({type: 'POST', url:php_url, script:true, cache:false, 
				success: function(data) { 
					 $('#category_levels_container_div').html(data);
					 $('#level_name').attr('value',"");
					} });
			
		}
		
		$('form').jqTransform({imgPath:'../images/forms'});

		var initial_z_index = 1000;
		  
		$('.jqTransformSelectWrapper').each( function(){
	
				$(this).css('z-index', initial_z_index--);
		});

		function validate(){
			var valid = true;
			var error_msg = "";
			if ($("#level_name").attr("value") == ""){
				valid = false;
				error_msg = "Level name required";
			};	
				
			if (!valid){
				$('#msg_text').html(error_msg);
				$('#error_msg').show('fast');
			}
			return valid;			
		}
		
</script>     

<style>
.formRight{
	width: 300px;
	
}
</style>
